<?php

namespace api\modules\v1\models\user;

use api\modules\v1\models\Common;
use common\models\CacheKeys;
use common\models\Functions;
use common\models\user\UserLoginStatistic;
use Yii;
use yii\base\Exception;

/**
 * Class UserLoginLog
 * @package api\modules\v1\models\employee
 */
class UserLoginErrorLog extends \common\models\user\UserLoginErrorLog
{
    public static function recordLog($loginName = '', $password = '', $msg = '', $type = 1) {
        if (empty($loginName)) {
            return true;
        }

        $insert['login_name'] = $loginName;
        $insert['password'] = $password;
        $insert['login_ip'] = Functions::getRemoteIp();
        $insert['login_time'] = date("Y-m-d H:i:s");
        $insert['login_date'] = date("Y-m-d");
        $insert['error_msg'] = $msg;
        $insert['type'] = $type;

        //todo  记录物理位置信息
        static::insertData($insert);
    }

    public static function chkTodayError ($one = 5, $two = 8, $three = 15) {
        $ip = Functions::getRemoteIp();
        $date = date("Y-m-d");
        $where['login_ip'] = Functions::getRemoteIp();
        $where['login_date'] = date("Y-m-d");
        $where['type'] = 1;
        $count = static::getCountByWhere($where);
        $redis = \Yii::$app->cache;

        $key = $date . "_1_" . $ip;
        $lock = $redis->get($key);
        if (!empty($lock)) {
            $lock = $lock == 1 ? "失败次数过多，请稍后尝试" : $lock;
            throw new Exception($lock, 2000);
        }

        if ($count == $one) {
            $redis->set($key, "失败次数过多，1分钟后再尝试", 60);
        }
        if ($count == $two) {
            $redis->set($key, "失败次数过多，5分钟后再尝试", 5 * 60);
        }
        if ($count >= $three) {
            throw new Exception('错误次数过多，今日无法登陆', 2000);
        }
    }


    public static function chkTodayLoginNameError ($loginName = '', $one = 5, $two = 8, $three = 15) {
        $ip = Functions::getRemoteIp();
        $date = date("Y-m-d");
        $where['login_name'] = $loginName;
        $where['login_date'] = date("Y-m-d");
        $where['type'] = 1;
        $count = static::getCountByWhere($where);
        $redis = \Yii::$app->cache;

        $key = $date . "_1_" . $ip;
        $lock = $redis->get($key);
        if (!empty($lock)) {
            $lock = $lock == 1 ? "失败次数过多，请稍后尝试" : $lock;
            throw new Exception($lock, 2000);
        }

        if ($count == $one) {
            $redis->set($key, "失败次数过多，1分钟后再尝试", 60);
        }
        if ($count == $two) {
            $redis->set($key, "失败次数过多，5分钟后再尝试", 5 * 60);
        }
        if ($count >= $three) {
            throw new Exception('错误次数过多，今日无法登陆', 2000);
        }
    }

    public static function chkPayPassword ($loginName = '') {
        $date = date("Y-m-d");
        $where['login_name'] = $loginName;
        $where['login_date'] = date("Y-m-d");
        $where['type'] = 2;
        $count = static::getCountByWhere($where);
        $redis = \Yii::$app->cache;

        $key = $date . "_2_" . $loginName;
        $lock = $redis->get($key);
        if (!empty($lock)) {
            $lock = $lock == 1 ? "失败次数过多，请稍后尝试" : $lock;
            throw new Exception($lock, 2000);
        }

        if ($count == 5) {
            $redis->set($key, "失败次数过多，请1分钟后尝试", 60);
        }
        if ($count == 8) {
            $redis->set($key, "失败次数过多，请5分钟后尝试", 5 * 60);
        }
        if ($count >= 15) {
            throw new Exception('错误次数过多，今日无法交易', 2000);
        }
    }
}
